Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sort discriminator entries by mapping order #1216

Merged
merged 2 commits into from
Mar 27, 2020

Conversation

zeapo
Copy link
Contributor

@zeapo zeapo commented Mar 26, 2020

The fix for #1111 (in 6e390f9) sadly broke the order of mapping entries.
Given the discriminator:

      discriminator:
        propertyName: petType
        x-limitToMapping: true
        mapping:
          dog: "#/components/schemas/Dog"
          cat: "#/components/schemas/Cat"
          doggy: "#/components/schemas/Dog"
          bee: "#/components/schemas/HoneyBee

The result is out of order:
image
Also, if we remove the Dog mappings, we end up with the Dog class between the mappings:
image

This PR adds a sort in case of the presence of a mapping. The behavior is quite simple:

  • Explicit mappings are prioritized to implicit mappings

        discriminator:
          propertyName: petType
          x-limitToMapping: true
          mapping:
            dog: "#/components/schemas/Dog"
            cat: "#/components/schemas/Cat"
            doggy: "#/components/schemas/Dog"
            bee: "#/components/schemas/HoneyBee"

    gives
    image

  • Explicit mappings are sorted by the order they were defined in the mapping

  • Implicit mappings are sorted alphabetically among themselves

        discriminator:
          propertyName: petType
          x-limitToMapping: true
          mapping:
            cat: "#/components/schemas/Cat"

    image
    In case no mapping is defined, the default behavior is kept.


if (indexLeft < 0 && indexRight < 0) {
// out of mapping, order by name
return left.name - right.name;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"test" - "test" === NaN

Maybe use localeCompare ?

@RomanHotsiy RomanHotsiy changed the title sort discriminator entries by mapping order fix: sort discriminator entries by mapping order Mar 27, 2020
@RomanHotsiy RomanHotsiy merged commit ac4f915 into Redocly:master Mar 27, 2020
@RomanHotsiy
Copy link
Member

LGTM 👍
Thanks!

jancerman pushed a commit to Kontent-ai-Learn/kontent-ai-learn-redoc that referenced this pull request Jun 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants